package com.seanLab.tool.TagSuggestModel;

import com.seanLab.domain.TagInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ModelKernelByWeighted extends ModelKernel {
    private double[] weights = new double[] {0.0, 1.0};

    public List<TagInfo> suggestTag(List<Feature> features, RawInfoType[] rawInfoTypes) {
        //TODO 完善权重, 分别记录source来源并全部返回
//        HashMap<String, Double> result = new HashMap<>();
//        for (int i = 0; i < 2; i++) {
//            FeatureInTags tags = (FeatureInTags)features;
//            for (TagInfo tag : tags.getFeature()) {
//                String text = tag.getTagName();
//                Double score = tag.getTagScore();
//                if (!result.containsKey(text)) {
//                    result.put(text, score * weights[i]);
//                } else {
//                    if (score * weights[i] > result.get(text)) {
//                        result.put(text, score * weights[i]);
//                    }
//                }
//            }
//        }
        ArrayList<TagInfo> finalTags = new ArrayList<>();
        for (int i = 0; i < rawInfoTypes.length; i++) {
            List<TagInfo> subTags = ((FeatureInTags)features.get(i)).getFeature();
            //TODO
            if (subTags.size() > 5) {
                subTags = subTags.subList(0, 5);
            }
            subTags = fillTagInfo(subTags, rawInfoTypes[i]);
            finalTags.addAll(subTags);
        }
//        return ((FeatureInTags) features.get(1)).getFeature();
        return finalTags;
    }

    public void setModelKernel(double[] weights) {
        this.weights = weights;
    }

    private List<TagInfo> fillTagInfo(List<TagInfo> rawTags, RawInfoType type) {
        if (type == null) {
            return rawTags;
        }
        for (TagInfo tag : rawTags) {
            tag.setTagID("tagId-" + UUID.randomUUID().toString());
            switch (type) {
                case TITLE:
                    tag.setTagSource("文本抽取-Title");
                    break;
                case CONTENT:
                    tag.setTagSource("文本抽取-Content");
                    break;
                case PARA_ABOVE_IMAGE:
                    tag.setTagSource("文本抽取-Para-Above");
                    break;
                case PARA_UNDER_IMAGE:
                    tag.setTagSource("文本抽取-Para-Under");
                    break;
                case DESC_OF_IMAGE:
                    tag.setTagSource("文本抽取-Description");
                    break;
                case SEMANTIC_OF_IMAGE:
                    tag.setTagSource("文本抽取-Semantic");
                    break;
                case TAG:
                    tag.setTagSource("文本抽取-Tag");
                    break;
            }
//            tag.setTagSource("文本抽取");
        }
        return rawTags;
    }
}
